Introducing the R Spatial Ecosystem

The basics

A few main packages are dedicated to spatial data handling like import/export, display, geoprocessing, spatial ststistics.

“Historical” Packages

These packages are still widely used.

Simple Features for R

  • sp, rgeos and rgdal functionnalities in one package.

  • Easier data handling, simpler objects.

  • Main author and maintainer: Edzer Pebesma (also sp author)

  • Compatibility with the pipe synthax and tidyverse operators.

  • First release: October 20, 2016

  • Website: Simple Features for R

sf Objects Data Structure

format sf

Using sf

Data Import

Reading layer `martinique' from data source `/home/tim/Documents/prz/flo/data/mtq/martinique.shp' using driver `ESRI Shapefile'
Simple feature collection with 34 features and 23 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 690574.4 ymin: 1592426 xmax: 736126.5 ymax: 1645660
epsg (SRID):    32620
proj4string:    +proj=utm +zone=20 +datum=WGS84 +units=m +no_defs

Data Display

Default

Only geometry

Distance Matrix

Units: m
          [,1]     [,2]      [,3]      [,4]      [,5]
[1,]     0.000 35297.56  3091.501 12131.617 17136.310
[2,] 35297.557     0.00 38332.602 25518.913 18605.249
[3,]  3091.501 38332.60     0.000 15094.702 20226.198
[4,] 12131.617 25518.91 15094.702     0.000  7177.011
[5,] 17136.310 18605.25 20226.198  7177.011     0.000

Mapping

Static Maps

Several solutions are available:

  • ggplot2 users can have a look to ggplot2 mapping features (geom_sf) that can mix nicely with ggspatial.
  • For more advanced mapping features in a ggplot2-like syntax have a look to tmap
  • cartography is based on base graphics and allow most of basic and advanced cartographic representations.
    Here we will focus on cartography. Full disclosure: the speaker is the maintener of cartography.

Using cartography

Data Preparation

Reading layer `OGRGeoJSON' from data source `/home/tim/Documents/prz/flo/data/rhone/seine_maritime.geojson' using driver `GeoJSON'
Simple feature collection with 718 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 0.06560943 ymin: 49.25139 xmax: 1.790225 ymax: 50.07121
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
Reading layer `OGRGeoJSON' from data source `/home/tim/Documents/prz/flo/data/rhone/dep.geojson' using driver `GeoJSON'
Simple feature collection with 96 features and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -5.138996 ymin: 41.36216 xmax: 9.559598 ymax: 51.089
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs

Dynamic/interactive maps

The 2 main packages dedicated to interactive mapping are leaflet and mapview.

leaflet

mapview

Spatial modelling and MISC

CRAN Task View: Analysis of Spatial Data

Exploration of sirene & osm db for restaurants

See file data_prep.R for data extraction.

Interactive visualisation

Explore datasets interactively

The default is not bad:

mapview is hightly customizable:

smooth density analysis

Define a function that compute smoothed restaurant density and map it.



reproducibility

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] mapview_2.4.0         maptools_0.9-2        raster_2.6-7         
 [4] sp_1.3-1              spatstat_1.56-0       rpart_4.1-13         
 [7] nlme_3.1-137          spatstat.data_1.3-1   SpatialPosition_1.2.0
[10] cartogram_0.1.0       cartography_2.1.1     sf_0.6-3             
[13] rmdformats_0.3.3      knitr_1.20           

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18         lattice_0.20-35      deldir_0.1-15       
 [4] png_0.1-7            class_7.3-14         rprojroot_1.3-2     
 [7] digest_0.6.15        foreach_1.4.4        mime_0.5            
[10] plyr_1.8.4           R6_2.2.2             backports_1.1.2     
[13] stats4_3.5.1         evaluate_0.11        e1071_1.6-8         
[16] tensor_1.5           highr_0.7            rstudioapi_0.7      
[19] miniUI_0.1.1.1       R.utils_2.6.0        R.oo_1.22.0         
[22] Matrix_1.2-14        goftest_1.1-1        rmarkdown_1.10      
[25] webshot_0.5.0        rgdal_1.3-3          stringr_1.3.1       
[28] questionr_0.6.3      foreign_0.8-70       htmlwidgets_1.2     
[31] munsell_0.5.0        polyclip_1.9-0       shiny_1.1.0         
[34] compiler_3.5.1       httpuv_1.4.4.2       xfun_0.3            
[37] base64enc_0.1-3      mgcv_1.8-24          rgeos_0.3-28        
[40] htmltools_0.3.6      bookdown_0.7         codetools_0.2-15    
[43] viridisLite_0.3.0    later_0.7.3          R.methodsS3_1.7.1   
[46] grid_3.5.1           jsonlite_1.5         spData_0.2.9.0      
[49] satellite_1.0.1      xtable_1.8-2         DBI_1.0.0           
[52] magrittr_1.5         formatR_1.5          scales_0.5.0        
[55] units_0.6-0          stringi_1.2.4        promises_1.0.1      
[58] leaflet_2.0.1        spatstat.utils_1.8-2 RColorBrewer_1.1-2  
[61] iterators_1.0.10     tools_3.5.1          gdalUtils_2.0.1.14  
[64] crosstalk_1.0.0      abind_1.4-5          yaml_2.2.0          
[67] colorspace_1.3-2     classInt_0.2-3